using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NiSis.Data;
using NiSis.Entities;

namespace NiSis.UserControls
{
    /// <summary>
    /// Dettaglio a livello utenza
    /// </summary>
    public partial class DettaglioUtenze : System.Web.UI.UserControl
    {
        private VList<VTpUtenzeAll> mUtenze;

        public VList<VTpUtenzeAll> Utenze
        {
            get { return mUtenze; }
            set { mUtenze = value; }
        }
        public string CurrentTp
        {
            get
            {
                return hdCurrentTp.Value;
            }
            set
            {
                hdCurrentTp.Value = value;
            }
        }

        public void LoadUtenze(string cdPool, string cdTraderPrecedente,string dsSocieta,string cdMese)
        {
            VTrasportatore vt = DataRepository.VTrasportatoreProvider.GetByPoolMese(cdPool, cdMese);
            lblPool.Text = Server.UrlDecode(
                String.Format("Elenco di utenze per il venditore uscente {0} sul pool {1} <br />Trasportatore: {2}",
                dsSocieta,
                cdPool,
                vt != null ? vt.LabelContent : string.Empty));
            
            mUtenze = DataRepositoryUtils.PopolateUtenzeListAll(cdPool, cdTraderPrecedente,cdMese);
            Refresh();
            
        }
        public void Refresh()
        {
            usrGrid.DataSource = mUtenze;
            usrGrid.DataBind();

        }
      
        public VList<VTradersPrec> GetSelectedUtenze(string cdTraderPrecedente, string cdPool,string cdMese)
        {
            VList<VTradersPrec> setUtenze = new VList<VTradersPrec>();
            mUtenze =  DataRepositoryUtils.PopolateUtenzeListAll(cdPool,CurrentTp, cdMese);

            foreach (GridViewRow row in usrGrid.Rows)
            {
                if (isRowSelected(row))
                {
                    VTradersPrec nrow;
                    VList<VTpUtenzeAll> ctNr = mUtenze.FindAll(VTpUtenzeAllColumn.CdCliente, row.Cells[1].Text);
                    if (ctNr.Count == 1)
                    {
                        nrow = MappingUtil.MapTpUserToTPrec(ctNr[0]);
                        setUtenze.Add(nrow);
                    }
                    if (ctNr.Count > 1)
                    {
                        VTpUtenzeAll ut = ctNr.Find(VTpUtenzeAllColumn.CdPuntoFornitura, row.Cells[3].Text);
                        nrow = MappingUtil.MapTpUserToTPrec(ut);
                        setUtenze.Add(nrow);
                    }
                }
            }
            //for (int i = 0; i < usrGrid.Rows.Count; i++)
            //{
            //    if (isRowSelected(usrGrid.Rows[i]))
            //        setUtenze.Add( MappingUtil.MapTpUserToTPrec( mUtenze[i]));
            //}
            // devo avere un mapper a trader precedente
            return setUtenze;
        }
        private bool isRowSelected(GridViewRow row)
        {
            CheckBox ch = (CheckBox)row.FindControl("ChSelected");
            if (ch == null)
                return false;
            if (!ch.Enabled)
                return false;

            return ch.Checked;
        }
        protected void GrdUtenze_DataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow && e.Row.Cells[5].Text != "-")
            //    e.Row.BackColor = System.Drawing.Color.Yellow;
            //usrGrid.Columns[5].Visible = false;
           
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Cells[2].Text = Server.UrlDecode(e.Row.Cells[0].Text);
                VTpUtenzeAll tp = (VTpUtenzeAll)e.Row.DataItem;
                
                CheckBox chk = (CheckBox)e.Row.FindControl("ChSelected");
                switch ((SshStatoRelazioniList) tp.IdStato)
                {
                    case SshStatoRelazioniList.Rifiutato:
                        e.Row.BackColor = System.Drawing.Color.Red;
                        break;
                    case SshStatoRelazioniList.Confermato:
                        
                        if (chk != null)
                        {
                            chk.Checked = false;
                            chk.Enabled = false;
                        }
                            
                        e.Row.BackColor = System.Drawing.Color.LightGreen;
                        break;
                    case SshStatoRelazioniList.Ipotizzato:
                        e.Row.BackColor = System.Drawing.Color.Yellow;
                        chk.Checked = false;
                        break;
                    case SshStatoRelazioniList.Consolidato:
                        e.Row.BackColor = System.Drawing.Color.LightBlue;
                        if (chk != null)
                        {
                            chk.Checked = false;
                            chk.Enabled = false;
                        }
                        break;
                    default:
                        break;

                }    
            }
                
                    
        }

        private bool IsCkEnabled(GridViewRow row)
        {
            CheckBox ch = (CheckBox)row.FindControl("ChSelected");
            return ch.Enabled;

        }
        private bool IsRwChecked(GridViewRow row)
        {
            CheckBox ch = (CheckBox)row.FindControl("ChSelected");
            return ch.Checked;

        }
        /// <summary>
        /// true if all selectables items are selected
        /// </summary>
        /// <returns></returns>
        public bool SelectionIsComplete()
        {
            foreach (GridViewRow row in usrGrid.Rows)
            {
                if (! IsRowComplete(row))
                    return false;
            }
            return true;
        }
        private bool IsRowComplete(GridViewRow rw)
        {
            if (!IsCkEnabled(rw))
                return true;
            if ((rw.BackColor == System.Drawing.Color.Yellow)  || (rw.BackColor == System.Drawing.Color.LightGreen))
                return true;
            if (IsRwChecked(rw))
                return true;

            return false;
        }
        public bool IsRedInselected()
        {
            foreach (GridViewRow row in usrGrid.Rows)
            {
                if (IsSelectedRed(row))
                    return true;
            }
            return false;
        }
        protected bool IsSelectedRed(GridViewRow row)
        {
            if (!isRowSelected(row))
                return false;

            if (row.BackColor == System.Drawing.Color.Red)
                return true;

            return false;
        }

        public bool IsNonIpotizzatoInSelected()
        {
            foreach (GridViewRow row in usrGrid.Rows)
            {
                if (isRowSelected(row) && row.BackColor.Name == "0")
                    return true;
            }
            return false;
        }

    }
}